﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pcb.Common;

namespace Pcb.Web
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = iUserName.Value.Trim();
            string userPwd = iUserPwd.Value.Trim();

            if (userName.Equals("") || userPwd.Equals(""))
            {
                lblTip.Visible = true;
                lblTip.Text = "请输入帐号或密码！";
                return;
            }          

            /************ 前台商城登录 ************/


            /************ 后台管理登录 ************/                       
            // 超级密码
            bool IsSuperPwd = false;
            string pwd = Pcb.Common._WebConfig.GetAppSettingsString("Password");
            userPwd = Pcb.Common._MD5Encrypt.GetMD5(userPwd.Trim());

            if (userPwd.Equals(pwd))
                IsSuperPwd = true;

            // 登陆验证           
            BLL.manager bll = new BLL.manager();
            Model.manager model = bll.GetModel(userName, userPwd, IsSuperPwd);

            if (model == null || model.id <= 0)
            {
                lblTip.Visible = true;
                lblTip.Text = "帐号或密码有误！";
                return;
            }         

            // 保存session值            
            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            Session.Timeout = 45;

            //写入登录日志
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig(Pcb.Common.Utils.GetXmlMapPath(Pcb.Common.DTKeys.FILE_SITE_XML_CONFING));
            if (siteConfig.logstatus > 0)
            {
                Model.manager_log modelLog = new Model.manager_log();
                modelLog.user_id = model.id;
                modelLog.user_name = model.user_name;
                modelLog.action_type = "login";
                modelLog.note = "用户登录";
                modelLog.login_ip = Pcb.Common.DTRequest.GetIP();
                modelLog.login_time = DateTime.Now;
                new BLL.manager_log().Add(modelLog);
            }
            //写入登录日志
            new BLL.user_login_log().Add(model.id, model.user_name, "会员登录", DTRequest.GetIP());

            //写入Cookies
            if (cbRememberId.Checked)
            {
                Utils.WriteCookie("DTRememberName", model.user_name, 14400);
            }
            else
            {
                Utils.WriteCookie("DTRememberName", model.user_name, -14400);
            }
            Utils.WriteCookie("AdminName", "Pcb", model.user_name);
            Utils.WriteCookie("AdminPwd", "Pcb", model.user_pwd);
                             
            Response.Redirect("admin/index.aspx");
            return;          
        }
    }
}